package com.thinksns.sociax.t4.android.fragment;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.thinksns.sociax.android.R;
import com.thinksns.sociax.api.Api;
import com.thinksns.sociax.constant.Verify;
import com.thinksns.sociax.db.UserSqlHelper;
import com.thinksns.sociax.modle.UserApprove;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.data.StaticInApp;
import com.thinksns.sociax.t4.android.erweima.ActivityScan;
import com.thinksns.sociax.t4.android.findpeople.ActivityFindPeopleDetails;
import com.thinksns.sociax.t4.android.gift.ActivityMyScore;
import com.thinksns.sociax.t4.android.setting.ActivitySetting;
import com.thinksns.sociax.t4.android.task.ActivityMedalPavilion;
import com.thinksns.sociax.t4.android.task.ActivityTaskCenter;
import com.thinksns.sociax.t4.android.user.ActivityFollowUser;
import com.thinksns.sociax.t4.android.user.ActivityUserInfo_2;
import com.thinksns.sociax.t4.android.verify.DoVerifyHomeActivity;
import com.thinksns.sociax.t4.android.verify.VerifyDetailsActivity;
import com.thinksns.sociax.t4.android.video.ToastUtils;
import com.thinksns.sociax.t4.model.ModelNotification;
import com.thinksns.sociax.t4.model.ModelUser;
import com.thinksns.sociax.t4.model.VerifyDetails;
import com.thinksns.sociax.t4.unit.UnitSociax;
import com.thinksns.sociax.thinksnsbase.bean.ListData;
import com.thinksns.sociax.thinksnsbase.bean.SociaxItem;
import com.thinksns.sociax.thinksnsbase.network.ApiHttpClient.HttpResponseListener;
import com.thinksns.sociax.thinksnsbase.utils.ActivityStack;
import com.thinksns.tschat.widget.UIImageLoader;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * 类说明： 我
 *
 * @author wz
 * @version 1.0
 * @date 2015-1-4
 */
public class FragmentMy extends FragmentSociax implements OnRefreshListener {
    private TextView tv_center;
    private ImageView iv_erweima;

    // my用到的变量
    private LinearLayout ll_myinfo;        // 我的信息
    private LinearLayout rl_mycollection, // 我的收藏
            rl_myrescore,// 我的积分
            rl_mydraft,// 草稿箱
            rl_userinfo, // 用户信息
            rl_myweibo,// 我的微博
            rl_myfollowing,// 我的关注
            rl_mytask,//我的任务
            rl_mymedal,//我的勋章馆
            rl_setting,// 设置
            rl_fengyun_bang, //风云榜
            rl_myzan;//赞

    private RelativeLayout rl_myfollowed;// 我的粉丝
    private ImageView img_user_header;    //用户头像
    private TextView tv_count_weibo,
            tv_count_follow,
            tv_count_followed;
    private TextView tv_my_username, tv_my_usertag;
    private boolean isMyInit = false;                    // 判断my是否初始化，如果初始化，则下次进入的时候可以去掉不用重复加载的内容
    private TextView tv_remind_follower;                // 新的粉丝
    private TextView tv_remind_draft; // 提示草稿数目

    private LinearLayout ll_user_group;
    private Handler mHandler;
    /*我的增加下拉刷新功能 5/8/2017 by leiyan*/
    private SmartRefreshLayout mSmartRefreshLayout; // 下拉刷新的 scrollView
    /*增加-我的认证 7.19.2017 leiyan*/
    private TextView mTvVerifyState;
    private LinearLayout mLlMyVerify;
    private int mVerifyState = -1;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    public void initView() {
        tv_center = (TextView) findViewById(R.id.tv_center);
        iv_erweima = (ImageView) findViewById(R.id.iv_erweima);
        tv_center.setText("我");
        // my
        rl_mycollection = (LinearLayout) findViewById(R.id.rl_mycollection);
        //草稿
        rl_mydraft = (LinearLayout) findViewById(R.id.rl_mydraft);
        //积分
        rl_myrescore = (LinearLayout) findViewById(R.id.rl_myscore);
        //个人信息
        rl_userinfo = (LinearLayout) findViewById(R.id.rl_userinfo);
        rl_setting = (LinearLayout) findViewById(R.id.rl_setting);

        rl_mytask = (LinearLayout) findViewById(R.id.rl_my_task);
        rl_mymedal = (LinearLayout) findViewById(R.id.rl_my_medal);

        rl_fengyun_bang = (LinearLayout) findViewById(R.id.rl_fengyun_bang);
        //rl_myzan = (LinearLayout) findViewById(R.id.rl_myzan);

        rl_myfollowing = (LinearLayout) findViewById(R.id.rl_myfollow);
        rl_myfollowed = (RelativeLayout) findViewById(R.id.rl_myfollowed);
        rl_myweibo = (LinearLayout) findViewById(R.id.rl_myweibo);
        img_user_header = (ImageView) findViewById(R.id.img_user_header);
        tv_count_follow = (TextView) findViewById(R.id.tv_count_follow);
        tv_count_followed = (TextView) findViewById(R.id.tv_count_followed);
        tv_count_weibo = (TextView) findViewById(R.id.tv_count_weibo);

        tv_my_username = (TextView) findViewById(R.id.tv_my_username);
        tv_my_usertag = (TextView) findViewById(R.id.tv_my_usertag);

        tv_remind_follower = (TextView) findViewById(R.id.tv_remind_follower);
        tv_remind_draft = (TextView) findViewById(R.id.tv_remind_draft);
        ll_user_group = (LinearLayout) findViewById(R.id.ll_uname_adn);
        mTvVerifyState = (TextView) findViewById(R.id.tv_my_verify_state);
        mLlMyVerify = (LinearLayout) findViewById(R.id.ll_my_verify);
        mSmartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.smart_refresh_layout);
        mSmartRefreshLayout.setEnableLoadmore(false); // 设置上拉加载不可用
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (mSmartRefreshLayout != null && mSmartRefreshLayout.isRefreshing()){
                    mSmartRefreshLayout.finishRefresh();
                }
                ModelUser user = (ModelUser) msg.obj;
                if (user != null) {
                    tv_count_weibo.setText(Integer.toString(user.getWeiboCount()));
                    tv_count_follow.setText(Integer.toString(user.getFollowersCount()));
                    tv_count_followed.setText(Integer.toString(user.getFollowedCount()));
                    showBasicInfo(user);
                }
            }
        };
    }

    @Override
    public void initIntentData() {
    }

    @Override
    public void initListener() {
        mSmartRefreshLayout.setOnRefreshListener(this);
        //个人二维码
        iv_erweima.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                ModelUser user = Thinksns.getMy();
                Intent intent = new Intent(getActivity(), ActivityScan.class);
                intent.putExtra("userImg", user.getFace());
                intent.putExtra("userName", user.getUserName());
                intent.putExtra("userIntro", user.getIntro());
                intent.putExtra("uid", user.getUid());
                /*增加为空判断，传入认证图标 8.14.2017*/
                UserApprove approve = user.getUserApprove();
                intent.putStringArrayListExtra("user_group", approve == null ? null : (ArrayList<String>) approve.getApprove());
                startActivity(intent);
            }

        });
        //我的设置
        rl_setting.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ActivitySetting.class);
                startActivity(intent);
            }
        });
        //我的分享
        rl_myweibo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(getActivity(), ActivityMyWeibo.class);
//                startActivity(intent);
            }
        });
        //个人信息
        rl_userinfo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ActivityUserInfo_2.class);
                startActivityForResult(intent, 200);
            }
        });
        //草稿箱
        rl_mydraft.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(getActivity(), ActivityMyDraft.class);
//                startActivity(intent);
            }
        });
        //任务中心
        rl_mytask.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ActivityTaskCenter.class);
                startActivity(intent);
            }
        });
        //勋章馆
        rl_mymedal.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ActivityStack.startActivity(getActivity(), ActivityMedalPavilion.class);
            }
        });

        rl_myrescore.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ActivityStack.startActivity(getActivity(), ActivityMyScore.class);
            }
        });

        //我的收藏
        rl_mycollection.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                ActivityStack.startActivity(getActivity(), ActivityCollectedWeibo.class);
            }
        });

        // 我关注的人
        rl_myfollowing.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ActivityFollowUser.class);
                intent.putExtra("uid", Thinksns.getMy().getUid());
                intent.putExtra("type", "following");
                startActivity(intent);

            }
        });

        //我的粉丝
        rl_myfollowed.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ActivityFollowUser.class);
                intent.putExtra("type", "follow");
                intent.putExtra("uid", Thinksns.getMy().getUid());
                startActivity(intent);
            }
        });
        //4.5版本个人中心改动
    /*    rl_myzan.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });*/

        rl_fengyun_bang.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),ActivityFindPeopleDetails.class);
                intent.putExtra("type", StaticInApp.FINDPEOPLE_TOPLIST);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });

        // 我的认证 7.20.2017
        mLlMyVerify.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Verify.STATE_NOTHING == mVerifyState) {
                    Intent intent = new Intent(v.getContext(), DoVerifyHomeActivity.class);
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(v.getContext(), VerifyDetailsActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void initData() {
        //更新用户基本资料
        showBasicInfo(Thinksns.getMy());
    }

    public void showBasicInfo(ModelUser user) {
        if (user != null) {
            //设置头像
            UIImageLoader.getInstance(getActivity()).displayImage(user.getFace(), img_user_header);
            //用户名
            tv_my_username.setText(user.getUserName());
            //用户简介
            tv_my_usertag.setText(TextUtils.isEmpty(user.getIntro()) ||
                    user.getIntro().equals("null") ? getResources().getString(R.string.empty_user_intro) : user.getIntro());
            //粉丝数
            tv_count_follow.setText(user.getFollowersCount() + "");
            //分享数
            tv_count_weibo.setText(user.getWeiboCount() + "");
            //关注数
            tv_count_followed.setText(user.getFollowedCount() + "");

            if (ll_user_group != null &&
                    user.getUserApprove() != null
                    && user.getUserApprove().getApprove().size() > 0) {
                UnitSociax unit = new UnitSociax(getActivity());
                unit.addUserGroup(user.getUserApprove().getApprove(), ll_user_group);
            } else {
                if (ll_user_group != null) {
                    ll_user_group.removeAllViews();
                }
            }
            showVerifyState(user);
        }
    }

    /**
     * 显示认证相关状态
     * @param user
     */
    private void showVerifyState(ModelUser user) {
       VerifyDetails verify =  user.getVerified();
        if (verify == null){
            mLlMyVerify.setVisibility(View.GONE);
            return;
        }
        mLlMyVerify.setVisibility(View.VISIBLE);
        mVerifyState = verify.getVerified();
        mTvVerifyState.setTextColor(ContextCompat.getColor(getActivity(), R.color.verify_normal));
        // 未认证
        if (Verify.STATE_NOTHING == mVerifyState){
            mTvVerifyState.setText(R.string.state_not_verify);
            // 审核中
        }else if(Verify.STATE_CHECKING == mVerifyState){
            mTvVerifyState.setText(R.string.state_verify_checking);
            // 已认证
        }else if(Verify.STATE_VERIFIED == mVerifyState){
            mTvVerifyState.setText(R.string.state_verify_pass);
           // 被驳回
        }else if(Verify.STATE_REJECT == mVerifyState){
            mTvVerifyState.setText(R.string.state_verify_reject);
            mTvVerifyState.setTextColor(ContextCompat.getColor(getActivity(), R.color.verify_reject));
        }
    }

    /**
     * 显示草稿数目提示
     */
    private void showDraftCount() {
        Thinksns app = (Thinksns) getActivity().getApplicationContext();
        if (app != null) {
            List<SociaxItem> drafts = app.getWeiboDraftSQL().getAllWeiboDraft(
                    20, 0);
            if (drafts.isEmpty()) {
                tv_remind_draft.setVisibility(View.GONE);
            } else {
                tv_remind_draft.setVisibility(View.VISIBLE);
                tv_remind_draft.setText(drafts.size() + "");
            }
            app.closeDb();
        }
    }

    private HttpResponseListener userListener = new HttpResponseListener() {
        @Override
        public void onSuccess(Object result) {
            ListData<SociaxItem> list = (ListData<SociaxItem>) result;
            if (list != null && list.size() == 1) {
                ModelUser user = (ModelUser) list.get(0);
                if (user.getUid() == Thinksns.getMy().getUid()) {
                    //更新本地用户数据
                    Thinksns.setMy(user);
                    UserSqlHelper.updateUser(user);
                }
                Message mainMsg = Message.obtain();
                mainMsg.obj = user;
                mHandler.sendMessage(mainMsg);
//                if (adapter != null && adapter instanceof AdapterUserInfoAlbum) {
//                    adapter.notifyDataSetChanged();
//                }
            }
        }

        @Override
        public void onError(Object result) {

        }
    };

    @Override
    public void onResume() {
        super.onResume();
        // 每次切换到该页面的时候刷新数据
        loadData();
        showDraftCount();
//        if (user != null && user.getUid() == Thinksns.getMy().getUid()) {
//            user = Thinksns.getMy();
//            showBasicInfo(user);
//        }
    }

    /**
     * 热门电台
     * 采用AsyncHttpClient的Post方式进行实现
     */
    public void getRefreshCount(final ModelUser user) {
        new Api.Users().show(user, new HttpResponseListener() {

            @Override
            public void onSuccess(Object result) {
                ListData<SociaxItem> list = (ListData<SociaxItem>) result;
                if (list != null && list.size() == 1)
                    showBasicInfo((ModelUser) list.get(0));
            }

            @Override
            public void onError(Object result) {
                ToastUtils.showToast(String.valueOf(result));
            }
        });
    }

    // 将byte数组转换成字符串
    public String byteToString(byte[] responseBody) {
        String strRead = null;
        try {
            strRead = new String(responseBody, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return strRead;
    }

    /**
     * 设置未读消息
     *
     * @param mdNotification
     */
    //	新粉丝超过99，应该显示99+
    public void setUnReadUi(ModelNotification mdNotification) {
        if (tv_remind_follower != null)
            if (mdNotification.getFollower() > 0) {
                tv_remind_follower.setText(mdNotification.getFollower() > 99 ? "99+" : (mdNotification.getFollower() + ""));
                tv_remind_follower.setVisibility(View.VISIBLE);
            } else {
                tv_remind_follower.setText(0 + "");
                tv_remind_follower.setVisibility(View.GONE);
            }
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    private void loadData(){
        new Api.Users().show(user, userListener);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        loadData();
    }
}
